#include <cairo.h>
#include <math.h>

static void
_do_draw_line (cairo_t *cr, double x, double d1, double d2)
{
	cairo_move_to (cr, -x, d1);
	cairo_line_to (cr, x, d2);
	cairo_arc_negative (cr, x, 0, d2, M_PI/2, M_PI*3/2);
	cairo_line_to (cr, -x, -d1);
	cairo_arc (cr, -x, 0, d1, M_PI/2, M_PI*3/2);
}

void
draw_line (cairo_t *cr, double x1, double y1, double d1,
		double x2, double y2, double d2)
{
	cairo_save (cr);
	cairo_translate (cr, (x1+x2)/2, (y1+y2)/2);

	double rotate_angle = atan((y2-y1)/(x2-x1));
	cairo_rotate (cr, rotate_angle);

	double sx = fabs((x2-x1)/2/cos(rotate_angle));
	double sy = fabs((y2-y1)/2);
	if (x1 != x2) {
		if (x1 < x2)
			_do_draw_line(cr, sx, d1/2, d2/2);
		else
			_do_draw_line(cr, sx, d2/2, d1/2);
	} else {
		_do_draw_line(cr, sy, d1/2, d2/2);
	}
	cairo_fill (cr);
	cairo_restore (cr);
}
